package com.vzenith;

import com.vzenith.cloud.http.HttpClient;
import com.vzenith.cloud.http.*;
import com.vzenith.cloud.util.BinaryUtil;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.InputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class Main {
    private static final String BASE_URL = "https://open.vzicloud.com";
    private static final String ACCESSKEY_ID = "";
    private static final String ACCESSKEY_SECRET = "";

    private static HttpResponse testDetectPlateCN(HttpClient client) {
        String appID = "2"; // 替换为自己的车牌识别应用ID
        String path = "/openapi/v1/prs_cn/user/apps/" + appID + "/plates/detect";

        InputStream inputStream = null;
        byte[] imageBytes = null;
        try {
            inputStream = new FileInputStream("C:\\Users\\victor\\Desktop\\test.jpg");
            imageBytes = new byte[inputStream.available()];
            inputStream.read(imageBytes);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        String image = BinaryUtil.toBase64String(imageBytes);
        String imageType = "BASE64";

        JSONObject data = new JSONObject();
        data.put("image", image);
        data.put("image_type", imageType);
        String body = data.toString();

        HttpRequest request = new HttpRequest(HttpMethod.POST, path);
        request.setJsonBody(body);

        return client.doRequest(request);
    }

    private static HttpResponse testGetAccessDeviceWebUrl(HttpClient client) {
        String path = "/openapi/v1/stp/user/devices/pdns/web";

        HttpRequest request = new HttpRequest(HttpMethod.GET, path);
        request.addParam("sn", "00501f6d-9f5c77d4");
        request.addParam("type", "json");

        return client.doRequest(request);
    }

    private static HttpResponse testGetAccessDeviceVideoUrl(HttpClient client) {
        String path = "/openapi/v1/stp/user/devices/vurl";

        HttpRequest request = new HttpRequest(HttpMethod.GET, path);
        request.addParam("sn", "00501f6d-9f5c77d4");
        request.addParam("type", "auto");

        return client.doRequest(request);
    }

    private static HttpResponse testGetMQTTConnectInfo(HttpClient client) {
        String path = "/openapi/v1/stp/clients/route";

        HttpRequest request = new HttpRequest(HttpMethod.GET, path);
        request.addParam("sid", "123456");

        return client.doRequest(request);
    }

    private static HttpResponse testScanDevices(HttpClient client) {
        String path = "/openapi/v1/stp/user/devices";

        JSONArray data = new JSONArray();
        // 构建设备状态为在线的筛选条件
        JSONObject exp1 = new JSONObject();
        exp1.put("k", "state");
        exp1.put("o", "eq");
        exp1.put("v", 0);
        // 构建设备系列不为RM的筛选条件
        JSONObject exp2 = new JSONObject();
        exp2.put("k", "series_name");
        exp2.put("o", "ne");
        exp2.put("v", "RM");

        data.put(exp1);
        data.put(exp2);

        String wheres = BinaryUtil.toBase64String(data.toString().getBytes(StandardCharsets.UTF_8));

        HttpRequest request = new HttpRequest(HttpMethod.GET, path);
        request.addParam("page", "1");
        request.addParam("size", "2");
        request.addParam("wheres", wheres);

        return client.doRequest(request);
    }

    private static HttpResponse testBindDevices(HttpClient client) {
        String path = "/openapi/v1/stp/user/devices";

        JSONArray data = new JSONArray();
        JSONObject sn1 = new JSONObject();
        sn1.put("sn", "xxxxxxxx-xxxxxxxx"); // 替换为真实的SN
        sn1.put("group_id", 0); // 替换为需要绑定的分组ID
        JSONObject sn2 = new JSONObject();
        sn2.put("sn", "yyyyyyyy-yyyyyyyy"); // 替换为真实的SN
        sn2.put("group_id", 0); // 替换为需要绑定的分组ID

        data.put(sn1);
        data.put(sn2);

        String body = data.toString();

        HttpRequest request = new HttpRequest(HttpMethod.POST, path);
        request.setJsonBody(body);

        return client.doRequest(request);
    }

    private static HttpResponse testUpdateDevices(HttpClient client) {
        String path = "/openapi/v1/stp/user/devices";

        JSONArray data = new JSONArray();
        JSONObject sn1 = new JSONObject();
        sn1.put("sn", "xxxxxxxx-xxxxxxxx"); // 替换为真实的SN
        sn1.put("remark", "测试");
        JSONObject sn2 = new JSONObject();
        sn2.put("sn", "yyyyyyyy-yyyyyyyy"); // 替换为真实的SN
        sn2.put("remark", "测试");

        data.put(sn1);
        data.put(sn2);

        String body = data.toString();

        HttpRequest request = new HttpRequest(HttpMethod.PUT, path);
        request.setJsonBody(body);

        return client.doRequest(request);
    }

    private static HttpResponse testUnbindDevices(HttpClient client) {
        String path = "/openapi/v1/stp/user/devices";

        JSONArray data = new JSONArray();
        JSONObject sn1 = new JSONObject();
        sn1.put("sn", "xxxxxxxx-xxxxxxxx"); // 替换为真实的SN
        JSONObject sn2 = new JSONObject();
        sn2.put("sn", "yyyyyyyy-yyyyyyyy"); // 替换为真实的SN

        data.put(sn1);
        data.put(sn2);

        String body = data.toString();

        HttpRequest request = new HttpRequest(HttpMethod.DELETE, path);
        request.setJsonBody(body);

        return client.doRequest(request);
    }

    public static void main(String[] args) {
        HttpClient client = new HttpClient(BASE_URL, ACCESSKEY_ID, ACCESSKEY_SECRET);

//        车牌识别
//        HttpResponse response = testDetectPlateCN(client);
//        获取远程访问设备网页地址
//        HttpResponse response = testGetAccessDeviceWebUrl(client);
//        获取远程访问设备视频地址
//        HttpResponse response = testGetAccessDeviceVideoUrl(client);
//        获取MQTT连接信息
//        HttpResponse response = testGetMQTTConnectInfo(client);
//        遍历设备
//        HttpResponse response = testScanDevices(client);
//        绑定设备
//        HttpResponse response = testBindDevices(client);
//        修改设备
//        HttpResponse response = testUpdateDevices(client);
//        解绑设备
        HttpResponse response = testUnbindDevices(client);

//       其他接口请参考以上示例和开发文档

        int status = response.getStatus();
        if (status == 200) {
            System.out.println("请求成功!");
        } else {
            System.out.println("请求失败!");
        }
        System.out.println(response.getBodyStr());
    }
}
